package com.hsh.gaoqianlianmeng.service.impl;

import com.hsh.gaoqianlianmeng.common.Result;
import com.hsh.gaoqianlianmeng.constants.RegexConstants;
import com.hsh.gaoqianlianmeng.constants.ResultConstants;
import com.hsh.gaoqianlianmeng.constants.filepath.ChuanMeiPathConstants;
import com.hsh.gaoqianlianmeng.constants.filepath.FileSuffer;
import com.hsh.gaoqianlianmeng.domain.*;
import com.hsh.gaoqianlianmeng.service.ChuanMeiService;
import com.hsh.gaoqianlianmeng.service.OrderService;
import com.hsh.gaoqianlianmeng.utils.DateUtils;
import com.hsh.gaoqianlianmeng.utils.MyBeanUtils;
import com.hsh.gaoqianlianmeng.utils.POIExcelUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChuanMeiServiceImpl implements ChuanMeiService {
    @Resource
    private OrderService orderService;

    /**
     * 读取 最新的 美团导出 文件
     * 美团导出  -> 传美导入
     *
     * @param dateStr
     * @param shopName
     * @param password
     * @return
     */
    @Override
    public Result mtExportToCmImport(String dateStr, String shopName, String password) {
        // 此次文档写入的主路径
        String pathPre = ChuanMeiPathConstants.CHUAN_MEI_FOLDER + shopName + "\\";

        // 美团导出日期格式前缀
        String mtExportDateStr = DateUtils.changeDateStrPatten(dateStr, "yyyy-MM-dd");
        // 读取 最新的 美团导出 文件
        List<MTExport> mtExports = POIExcelUtils.readNewest(pathPre + ChuanMeiPathConstants.MT_EXPORT_FOLDER,
                ChuanMeiPathConstants.MT_EXPORT_NAME_PRE + mtExportDateStr, MTExport.class, password);
        if (mtExports.size() == 0) {
            return Result.fail(ResultConstants.FILE_IS_NOT_EXIST);
        }

        // 将 美团导出 -> 传美导入
        List<CMImport> cmImports = MyBeanUtils.copyList(mtExports, CMImport.class);
        cmImports.forEach(CMImport::init);
        cmImports = orderService.mergeOrder(cmImports);

        // 写入传美导入
        int write = POIExcelUtils.distinctWrite(pathPre + ChuanMeiPathConstants.CM_IMPORT_FOLDER
                        + ChuanMeiPathConstants.CM_IMPORT_NAME_PRE + dateStr + "-" + DateUtils.getNowDateTimeStr() + FileSuffer.XLSX_SUF
                , cmImports, CMImport.class);
        return Result.create(write);
    }

    /**
     * 传美导出  -> 美团导入
     *
     * @param dateStr
     * @param shopName
     * @return
     */
    @Override
    public Result cmExportToMtImport(String dateStr, String shopId, String expressCompanyId, String shopName) {
        // 此次文档写入的主路径
        String pathPre = ChuanMeiPathConstants.CHUAN_MEI_FOLDER + shopName + "\\";

        // 读取 传美导出 文件
        List<CMExport> cmExports = POIExcelUtils.reads(pathPre + ChuanMeiPathConstants.CM_EXPORT_FOLDER
                , ChuanMeiPathConstants.CM_EXPORT_NAME_PRE + dateStr, CMExport.class);
        // 去重
        cmExports = cmExports.stream().distinct().collect(Collectors.toList());
        cmExports = splitMeagreOrder(cmExports);

        // 将 传美导出 -> 美团导入
        List<MTImport> mtImports = MyBeanUtils.copyList(cmExports, MTImport.class);
        mtImports.stream().forEach(v -> v.setShopId(shopId).setExpressCompanyId(expressCompanyId));
        // 写入 美团导入 文件
        int writeMtImport = POIExcelUtils.distinctWrite(pathPre + ChuanMeiPathConstants.MT_IMPORT_FOLDER + ChuanMeiPathConstants.MT_IMPORT_NAME_PRE + dateStr + FileSuffer.XLSX_SUF
                , mtImports, MTImport.class);

        // 美团导入 -> 揽收文件
        List<Send> sends = MyBeanUtils.copyList(mtImports, Send.class);
        // 写入 揽收 文件
        int writeSend = POIExcelUtils.distinctWrite(pathPre + ChuanMeiPathConstants.SEND_FOLDER + ChuanMeiPathConstants.SEND_NAME_PRE + dateStr + FileSuffer.XLSX_SUF
                , sends, Send.class);
        return Result.create(writeMtImport);
    }

    /**
     * 将已合并的订单拆开
     *
     * @param cmExports
     * @return
     */
    private List<CMExport> splitMeagreOrder(List<CMExport> cmExports) {
        List<CMExport> newList = new ArrayList<>();
        for (CMExport cm : cmExports) {
            String[] nos = cm.getNo().split(RegexConstants.SEPARATOR_REGEX);
            for (String no : nos) {
                newList.add(cm.setNo(no));
            }
        }
        return newList;
    }
}
